const util = require("../../utils/util");
var app = getApp();
Page({
    data: {
        userInfo: app.globalData.userInfo,
        //是否在进行下拉刷新,默认否
        pullDownRefresh: false,
        noticeList: [],
        carId: 0,
        dayTime: '',
        title: '吾计备忘录',
        desc: '无论多么风光或是糟糕、快乐或者失意，过了这一刻都成往事。除了你自己，谁又会更在乎？过不去的不是一个坎，而是你心中的执迷。'
    },
    onLoad: function (options) {
        var self = this;
        //检查token是否存在
        var userInfo = util.getStorageSync("userInfo");
        if (userInfo != null && app.globalData.userInfo) {
            self.setData({
                userInfo: app.globalData.userInfo,
            });
        }
        var carId = options.carId;
        var newDate = util.dateUtil.format(new Date(), 'Y-M-D');
        self.setData({
            carId: carId,
            dayTime: newDate
        });
    },
    onShow: function () {
        this.getCarNoticeHistory();
    },
    onPullDownRefresh: function () {
        this.setData({
            pullDownRefresh: true
        });
        this.getCarNoticeHistory();
    },
    getCarNoticeHistory: function () {
        var self = this;
        // 请求数据，并渲染
        wx.request({
            url: app.globalData.api + '/CarNotice/GetCarNoticeHistory',
            data: {
                token: app.globalData.userInfo.token,
                carId: self.data.carId,
                dayTime: self.data.dayTime
            },
            method: 'GET',
            success: function (res) {
                if (res.data.resultCode == 0) {
                    self.setData({
                        noticeList: res.data.data.noticeList
                    });
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    });
                }
            },
            complete: function () {
                //停止刷新
                if (self.data.pullDownRefresh) {
                    wx.stopPullDownRefresh();
                }
            }
        });
    },
    bindDayTimeChange: function (e) {
        this.setData({
            dayTime: e.detail.value
        });
        this.getCarNoticeHistory();
    },

    onShareAppMessage: function () {
        return {
            title: this.data.title,
            desc: this.data.desc,
            path: 'pages/carnotes/index',
            imageUrl: app.globalData.shareImgUrl
        };
    }
});